import { login, getUserProfile, getUserDetailById } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
export default {
  //命名空间
  namespaced: true,
  state: {
    // 保存token ,优先用本地token
    token: getToken() || "",
    //用户基本信息
    user: {},
  },
  mutations: {
    //更新token
    userToken(state, token) {
      state.token = token;
      setToken(token);
    },
    // 删除token
    removeToken(state) {
      state.token = "";
      removeToken();
    },
    //更新用户信息
    userInfo(state, user) {
      state.user = user;
    },
    //清空用户信息
    clearUser(state) {
      state.user = {};
    },
  },
  actions: {
    async getUserToken(context, loginForm) {
      const res = await login(loginForm);
      context.commit("userToken", res.data);
    },
    async getUserInfo(context) {
      const info = await getUserProfile();
      //根据id获取员工基本信息
      const rs = await getUserDetailById(info.data.userId);
      context.commit("userInfo", { ...info.data, ...rs.data });
    },
    //退出清空token 用户信息
    logout(context) {
      context.commit("removeToken");
      context.commit("clearUser");
    },
  },
};
